<template>
  <el-dialog
    :before-close="close"
    :width="dialogWidth"
    title="修改密码"
    :visible.sync="modifyPassword"
    v-dialogDrag
  >
    <el-form :model="modifyPasswordForm" ref="modifyPasswordForm" :rules="rules">
      <el-form-item label="管理员名：" prop="name" :label-width="formLabelWidth">
        <el-input v-model="modifyPasswordForm.name" autocomplete="off" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="原 密 码：" prop="originalPassword" :label-width="formLabelWidth">
        <el-input
          v-model="modifyPasswordForm.originalPassword"
          autocomplete="off"
          placeholder="请输入原 密 码"
        ></el-input>
      </el-form-item>
      <el-form-item label="新 密 码：" prop="newPassword" :label-width="formLabelWidth">
        <el-input
          v-model="modifyPasswordForm.newPassword"
          autocomplete="off"
          placeholder="请输入新 密 码"
        ></el-input>
      </el-form-item>
      <el-form-item label="确定密码：" prop="rePassword" :label-width="formLabelWidth">
        <el-input
          v-model="modifyPasswordForm.rePassword"
          autocomplete="off"
          placeholder="请输入确定密码"
          @keyup.enter.native="submitModify('modifyPasswordForm')"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer align-center">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submitModify('modifyPasswordForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import "../utils/drag.js";

export default {
  name: "modify_password",
  data() {
    var validateNewPassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入新密码！"));
      } else if (value.length < 6) {
        return callback(new Error("密码格式错误！"));
      } else {
        callback();
      }
    };
    var validateRePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入确认密码！"));
      } else if (value.length < 6) {
        return callback(new Error("密码格式错误！"));
      } else if (
        this.modifyPasswordForm.newPassword !==
        this.modifyPasswordForm.rePassword
      ) {
        callback(new Error("两次密码不一致！"));
      } else {
        callback();
      }
    };
    var validateOrPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入原密码！"));
      } else if (value.length < 6) {
        return callback(new Error("密码格式错误！"));
      } else {
        callback();
      }
    };
    return {
      modifyPasswordForm: {
        name: "duofuni",
        newPassword: "",
        rePassword: "",
        originalPassword: ""
      },
      rules: {
        newPassword: [{ validator: validateNewPassword, trigger: "blur" }],
        originalPassword: [{ validator: validateOrPassword, trigger: "blur" }],
        rePassword: [{ validator: validateRePassword, trigger: "blur" }]
      },
      loginUser: JSON.parse(sessionStorage.getItem("loginUser")),
      formLabelWidth: "100px",
      dialogWidth: "400px"
    };
  },
  created() {},
  computed: {},
  props: ["modifyPassword"],
  methods: {
    close() {
      this.$emit("close");
    },
    submitModify(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) {
          this.$message({
            type: "waring",
            message: "修改失败！",
            showClose: true
          });
          return false;
        }
        this.$axios
          .put("/api/users/resetPassword/" + this.loginUser.id, {
            newPassword: this.modifyPasswordForm.newPassword,
            originalPassword: this.modifyPasswordForm.originalPassword
          })
          .then(data => {
            if (data.status) {
              this.$alert("请重新登陆！", "密码修改成功", {
                confirmButtonText: "确定",
                callback: () => {
                  this.$router.push("/login");
                }
              });
            } else {
              this.$message({
                type: "waring",
                message: data.msg,
                showClose: true
              });
              return false;
            }
          });
      });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
